package com.example.beikeyigou.goods;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.beikeyigou.R;
import com.example.beikeyigou.utils.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

public class GoodsType2Adapter extends BaseAdapter {
    Context context;
    List<GoodsBean.DataBean> data;

    public GoodsType2Adapter(Context context, List<GoodsBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GoodsType2Adapter.VH vh = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_guess_like, parent, false);
            vh = new GoodsType2Adapter.VH(convertView);
            convertView.setTag(vh);
        } else {
            vh = (GoodsType2Adapter.VH)convertView.getTag();
        }
        GoodsBean.DataBean dataBean = data.get(position);
        vh.titleTv.setText(dataBean.getName());
        vh.priceTv.setText("￥"+dataBean.getPrice());
        String photo = dataBean.getPhoto();
        if (!TextUtils.isEmpty(photo)) {
            String photoUrl = URLUtils.PUBLIC_URL+photo;
            Picasso.with(context).load(photoUrl).into(vh.iv);
        }
        return convertView;
    }

    class VH {
        TextView titleTv, priceTv;
        ImageView iv;

        public VH(View v) {
            titleTv = v.findViewById(R.id.item_guess_tv_title);
            priceTv = v.findViewById(R.id.item_guess_tv_price);
            iv = v.findViewById(R.id.item_guess_img);
        }
    }
}
